// pages/goods_detail/index.js
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodId: 0,
    details: {},
    swiperImages: [],
    options: {
      interval: 3000,
      autoplay: true,
      circular: true,
      indicatorDots: true,
      indicatorActiveColor: '#fff',
    },
    isShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodId: options.id,
    });
    this.getDetails();
  },

  getDetails() {
    app.wxRequest({
        url: '/goods/detail?goods_id=' + this.data.goodId,
      })
      .then((res) => {
        // console.log(res);
        const images = this.setImages(res);
        this.setData({
          details: res.data.message,
          swiperImages: images,
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  setImages(res) {
    const {
      pics
    } = res.data.message;
    const swiperImages = this.data.swiperImages;
    pics.forEach((val, i, arr) => {
      const obj = {
        goods_id: val.goods_id,
        pics_id: val.pics_id,
        image_src: val.pics_big,
      };
      swiperImages.push(obj);
    });
    return swiperImages;
  },
  add() {
    const that = this;
    wx.cloud
      .callFunction({
        name: 'cart',
        data: {
          type: 'updCart',
          id: that.data.details.goods_id,
          from: 'details',
          goods: {
            cart_id: that.data.details.cat_id,
            goods_name: that.data.details.goods_name,
            goods_big_logo: that.data.details.goods_big_logo,
            goods_cat: that.data.details.goods_cat,
            goods_id: that.data.details.goods_id,
            goods_price: that.data.details.goods_price,
            update_time: new Date(),
            add_time: new Date(),
            num: 1,
            isChecked: false,
            isBuyed: false,
          },
        },
      })
      .then((res) => {
        console.log(res);
        wx.showToast({
          title: '添加成功',
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  buy() {
    console.log('buy');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},

  /**
   * 页面滚动监听
   * @param {*} e
   */
  onPageScroll: function (e) {
    let {
      isShow
    } = this.data;
    if (e.scrollTop > 246) isShow = true;
    else isShow = false;
    this.setData({
      isShow,
    });
  },
});